package com.esteel.settlement.pa.controller;

import java.util.Calendar;
import java.util.Date;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.esteel.settlement.config.pa.PaTemplate;
import com.esteel.settlement.config.pa.PacketFactory;
import com.esteel.settlement.entity.TbSettPlatInfo;
import com.esteel.settlement.pa.packet.Answer;
import com.esteel.settlement.pa.packet.CheckBalanceOfBankAccountPB1010;
import com.esteel.settlement.pa.packet.CheckMemberAccountBankBalancePB1020;
import com.esteel.settlement.pa.packet.CheckMemberInformationByTimePB1016;
import com.esteel.settlement.pa.packet.InMoneyPB1316;
import com.esteel.settlement.pa.packet.OutMoneyPB1318;
import com.esteel.settlement.pa.packet.PaPacket;
import com.esteel.settlement.pa.packet.PlatformChargeRefundPB1030;
import com.esteel.settlement.pa.packet.PlatformOperationPaymentPB1331;
import com.esteel.settlement.pa.packet.PlatformPayCollectPB1031;
import com.esteel.settlement.pa.packet.QueryMemberInOutMoneyFlowByTimePB1325;
import com.esteel.settlement.pa.packet.QueryMemberTransactionFlowByTimePB1324;
import com.esteel.settlement.pa.packet.QueryPayInstructionStatusPB1327;
import com.esteel.settlement.pa.packet.RegulatoryAccountInformationQueryPB1021;
import com.esteel.settlement.pa.packet.RspBP1010;
import com.esteel.settlement.pa.packet.RspBP1016;
import com.esteel.settlement.pa.packet.RspBP1020;
import com.esteel.settlement.pa.packet.RspBP1021;
import com.esteel.settlement.pa.packet.RspBP1029;
import com.esteel.settlement.pa.packet.RspBP1030;
import com.esteel.settlement.pa.packet.RspBP1031;
import com.esteel.settlement.pa.packet.RspBP1303;
import com.esteel.settlement.pa.packet.RspBP1316;
import com.esteel.settlement.pa.packet.RspBP1324;
import com.esteel.settlement.pa.packet.RspBP1325;
import com.esteel.settlement.pa.packet.RspBP1327;
import com.esteel.settlement.pa.packet.RspBP1328;
import com.esteel.settlement.pa.packet.RspBP1331;
import com.esteel.settlement.pa.packet.SubAccountFreezeThawPB1029;
import com.esteel.settlement.pa.packet.SubAccountReviewPayPB1328;
import com.esteel.settlement.service.SettFundService;
import com.esteel.settlement.service.SettPlatInfoService;

/**
 * 接收b2bic返回数据的程序
 * 
 * @author zhangxiuzhi
 *
 */
@Controller
public class TestController {

	@Autowired
	private PaTemplate paTemplate;

	@Autowired
	private SettPlatInfoService settPlatInfoService;

	@Autowired
	private SettFundService settFundService;
	
	@Autowired
	private PacketFactory packetFactory;

	@RequestMapping("/parj")
	@ResponseBody
	public Answer testrj() {
//		InMoneyPB1316 pb1316 = new InMoneyPB1316();

		InMoneyPB1316 pb1316 = packetFactory.getPacket(InMoneyPB1316.class);
		
		pb1316.setCustAcctId("888800117401651");
		pb1316.setThirdCustId("diangang05");
		pb1316.setIdType("53");
		pb1316.setIdCode("12349435-6");
		pb1316.setTranAmount(1);
		pb1316.setInAcctId("11014839149004");
		pb1316.setInAcctIdName("平安测试六零零零六四二零九一八三");
		
		pb1316.setThirdLogNo("i" + new Date().getTime());


		RspBP1316 rsp = paTemplate.execute(pb1316, RspBP1316.class);
		return rsp;

	}

	@RequestMapping("/pacj")
	@ResponseBody
	public Answer testcj() {
		
		OutMoneyPB1318 pb = packetFactory.getPacket(OutMoneyPB1318.class);

		pb.setThirdCustId("diangang05");
		pb.setIdType("52");
		pb.setIdCode("12349435-6");
		pb.setTranOutType(1);
		pb.setCustAcctId("888800117401651");
		pb.setCustName("平安测试六零零零六四二零九一八三");
		pb.setTranType(1);
		pb.setOutAcctId("11014839149004");
		pb.setOutAcctIdName("平安测试六零零零六四二零九一八三");
		pb.setOutAcctIdBankName("平安银行");
		pb.setTranAmount(1);


		pb.setThirdLogNo("o" + new Date().getTime());


		RspBP1303 rsp = paTemplate.execute(pb, RspBP1303.class);
		return rsp;

	}

	@RequestMapping("/pa1021")
	@ResponseBody
	public Answer test1021() {

		RegulatoryAccountInformationQueryPB1021 pb = packetFactory.getPacket(RegulatoryAccountInformationQueryPB1021.class);
//		pb.setTranFunc("1021");
		pb.setThirdLogNo("o" + new Date().getTime());
		RspBP1021 rsp = paTemplate.execute(pb, RspBP1021.class);

		TbSettPlatInfo tbSettPlatInfo = new TbSettPlatInfo();
		BeanUtils.copyProperties(rsp, tbSettPlatInfo);
		settPlatInfoService.save(tbSettPlatInfo);

		return rsp;
	}

	@RequestMapping("/pa1029")
	@ResponseBody
	public Answer test1029() {

		SubAccountFreezeThawPB1029 pb = packetFactory.getPacket(SubAccountFreezeThawPB1029.class);

		pb.setThirdLogNo("o" + new Date().getTime());
		pb.setFuncFlag(SubAccountFreezeThawPB1029.FREEZE);
		pb.setCustAcctId("888800117402033");
		pb.setThirdCustId("diangang06");
		pb.setTranAmount(1);
		pb.setThirdHtId("1029" + new Date().getTime());

		RspBP1029 rsp = paTemplate.execute(pb, RspBP1029.class);

		return rsp;
	}

	@RequestMapping("/pa1030")
	@ResponseBody
	public Answer test1030() {

		PlatformChargeRefundPB1030 pb = packetFactory.getPacket(PlatformChargeRefundPB1030.class);


		pb.setThirdLogNo("o" + new Date().getTime());

		pb.setFuncFlag(1);
		pb.setCustAcctId("888800117402033");
		pb.setThirdCustId("diangang06");
		pb.setTranAmount(1);
		pb.setThirdHtId("1030" + new Date().getTime());

		RspBP1030 rsp = paTemplate.execute(pb, RspBP1030.class);

		return rsp;

	}
	
	
	@RequestMapping("/pa1031")
	@ResponseBody
	public Answer test1031() {

	
		PlatformPayCollectPB1031 pb =  packetFactory.getPacket(PlatformPayCollectPB1031.class);

		pb.setThirdLogNo("o" + new Date().getTime());
		pb.setFuncFlag(SubAccountFreezeThawPB1029.FREEZE);
		pb.setCustAcctId("888800117402033");
		pb.setThirdCustId("diangang06");
		pb.setTranAmount(1);
		pb.setThirdHtId("1031" + new Date().getTime());

		RspBP1031 rsp = paTemplate.execute(pb, RspBP1031.class);

		return rsp;

	}
	

	
	
	@RequestMapping("/pa1020")
	@ResponseBody
	public Answer test1020() {

	
		CheckMemberAccountBankBalancePB1020 pb =  packetFactory.getPacket(CheckMemberAccountBankBalancePB1020.class);

		pb.setThirdLogNo("o" + new Date().getTime());
		
		pb.setCustAcctId("888800117402033");
		pb.setThirdCustId("diangang06");
		pb.setCustName("平安测试六零零零六四四四六五九九");
		pb.setAcctNo("11014847233002");
		
		RspBP1020 rsp = paTemplate.execute(pb, RspBP1020.class);

		return rsp;

	}
	
	
	@RequestMapping("/pa1328")
	@ResponseBody
	public Answer test1328() {

	
		SubAccountReviewPayPB1328 pb =  packetFactory.getPacket(SubAccountReviewPayPB1328.class);

		pb.setFuncFlag(1);
		pb.setOutCustAcctId("888800117402033");
		pb.setOutThirdCustId("diangang06");
		
		pb.setInCustAcctId("888800117401651");
		pb.setInThirdCustId("diangang05");
		pb.setTranAmount(10);
		pb.setHandFee(1);
		pb.setPaySerialNo("pay00002");
		pb.setThirdHtId("htid00002");
		
		pb.setThirdLogNo("o" + new Date().getTime());
		

		
		RspBP1328 rsp = paTemplate.execute(pb, RspBP1328.class);

		return rsp;

	}
	
	
	@RequestMapping("/pa1327")
	@ResponseBody
	public Answer test1327(String no) {

		QueryPayInstructionStatusPB1327 pb =  packetFactory.getPacket(QueryPayInstructionStatusPB1327.class);
		pb.setPaySerialNo(no);
		pb.setThirdLogNo("o" + new Date().getTime());
		RspBP1327 rsp = paTemplate.execute(pb, RspBP1327.class);

		return rsp;

	}
	@RequestMapping("/pa1010")
	@ResponseBody
	public PaPacket test1010(){
		CheckBalanceOfBankAccountPB1010 pb = packetFactory.getPacket(CheckBalanceOfBankAccountPB1010.class);
		//pb.setThirdCustId("diangang06");
		pb.setSelectFlag(1);
		pb.setPageNum(1);
		pb.setThirdLogNo("o" + new Date().getTime());
		RspBP1010 rsp = paTemplate.execute(pb, RspBP1010.class);
//		
//		List<Query1010Array> list = rsp.getQuery1010Array();
//		for (Query1010Array obj:list){
//			TbSettFundPK pk = new TbSettFundPK();
//			pk.setCustAcctId(obj.getCustAcctId());
//			pk.setThirdCustId(obj.getThirdCustId());
//			TbSettFund fund = settFundService.findOne(pk);
//			if (fund==null){
//				fund = new TbSettFund();
//				fund.setId(pk);
//			}
//			
//
//			
//			BeanUtils.copyProperties(obj, fund);
//			fund.setSupAcctId(pb.getSupAcctId());
//			fund.setSettDate(new Date());
//			settFundService.save(fund);
//			
//		}
		
		return rsp;
	}
	
	@RequestMapping("/pa1016")
	@ResponseBody
	public PaPacket test1016(){
		CheckMemberInformationByTimePB1016 pb = packetFactory.getPacket(CheckMemberInformationByTimePB1016.class);

		pb.setPageNum(1);
		
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.YEAR, -1);
		pb.setBeginDate(cal.getTime());
		pb.setEndDate(new Date());
		pb.setThirdLogNo("o" + new Date().getTime());
		RspBP1016 rsp = paTemplate.execute(pb, RspBP1016.class);
		return rsp;
	} 
	
	/**
	 * 查询会员交易流水
	 * @return
	 */
	@RequestMapping("/pa1324")
	@ResponseBody
	public PaPacket test1324(int page){
		QueryMemberTransactionFlowByTimePB1324 pb = packetFactory.getPacket(QueryMemberTransactionFlowByTimePB1324.class);

		pb.setPageNum(page);
		
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.YEAR, -1);
		pb.setBeginDate(cal.getTime());
		pb.setEndDate(new Date());
		pb.setThirdLogNo("o" + new Date().getTime());
		RspBP1324 rsp = paTemplate.execute(pb, RspBP1324.class);
		return rsp;
	} 
	

	/**
	 * 查询会员出入金流水信息
	 * @return
	 */
	@RequestMapping("/pa1325")
	@ResponseBody
	public PaPacket test1325(){
		QueryMemberInOutMoneyFlowByTimePB1325 pb = packetFactory.getPacket(QueryMemberInOutMoneyFlowByTimePB1325.class);

		pb.setPageNum(1);
		
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.YEAR, -1);
		pb.setBeginDate(cal.getTime());
		pb.setEndDate(new Date());
		pb.setThirdLogNo("o" + new Date().getTime());
		RspBP1325 rsp = paTemplate.execute(pb, RspBP1325.class);
		return rsp;
	}
	
	@RequestMapping("/pa1331")
	@ResponseBody
	public PaPacket test1331(){
		PlatformOperationPaymentPB1331 pb = packetFactory.getPacket(PlatformOperationPaymentPB1331.class);

		pb.setThirdLogNo("o" + new Date().getTime());
		
		pb.setFuncFlag(1);
		pb.setPaySerialNo("pay0003");
		pb.setThirdHtId("pay0003");
		pb.setPayAmount(10);
		pb.setPayFee(1);
		RspBP1331 rsp = paTemplate.execute(pb, RspBP1331.class);
		return rsp;
	}
	
	@RequestMapping("/sync1")
	@ResponseBody
	public String syncFundInfo(String custAcctId){
		settFundService.syncFundInfo(custAcctId);
		return "ok";
	}
}
